package net.lovesrc.allin.biz.dida;

import com.google.common.collect.Sets;
import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;

public abstract class RideHelper {

    public static Set<ListingHolder> match(Ride ride, List<Listing> listings) {
        Date now = new Date();
        LocalTime rideSTime1 = ride.getTargetSTime1();
        LocalTime rideSTime2 = ride.getTargetSTime2();
        Double rideSLat = ride.getSLat();
        Double rideSLng = ride.getSLng();
        Double rideELat = ride.getELat();
        Double rideELng = ride.getELng();
        Integer rideSDistance = ride.getSDistance();
        Integer rideEDistance = ride.getEDistance();
        Date tomorrow = DateUtils.truncate(DateUtils.addDays(now, 1), Calendar.DAY_OF_MONTH);
        Date tomorrow2 = DateUtils.truncate(DateUtils.addDays(now, 2), Calendar.DAY_OF_MONTH);
        Set<ListingHolder> holders = Sets.newTreeSet();
        for (Listing listing : listings) {
            Date planStartDate;
            try {
                planStartDate = DateUtils.parseDate(listing.getPlanStartTime(), "yyyyMMddHHmmss");
            } catch (ParseException e) {
                e.printStackTrace();
                planStartDate = new Date();
            }
            LocalTime planStartTime = LocalTime.parse(listing.getPlanStartTime().substring(listing.getPlanStartTime().length() - 6), DateTimeFormatter.ofPattern("HHmmss"));
            LocalTime startTime = planStartTime.minusMinutes(listing.getTimeScaleMins());
            LocalTime endTime = planStartTime.plusMinutes(listing.getTimeScaleMins());
            Poi fromPoi = listing.getFromPoi();
            int sDistance = GpsUtils.getDistance(rideSLat, rideSLng, fromPoi.getLatitude(), fromPoi.getLongitude()).intValue();
            Poi toPoi = listing.getToPoi();
            int eDistance = GpsUtils.getDistance(rideELat, rideELng, toPoi.getLatitude(), toPoi.getLongitude()).intValue();

            if (listing.getMatchPercent() >= 90) {
                // is okay
            } else {
                if (planStartDate.after(tomorrow2)) {
                    continue;
                }
                if ("cancelled".equalsIgnoreCase(listing.getStatus())) {
                    continue;
                }
                if ("paid".equalsIgnoreCase(listing.getStatus())) {
                    continue;
                }
                if (listing.getDriverReceivedPrice().intValue() < 15) {
                    continue;
                }
                if (endTime.isBefore(rideSTime1.minusMinutes(1)) || startTime.isAfter(rideSTime2.plusMinutes(1))) {
                    continue;
                }
                if (rideSDistance != null && sDistance > rideSDistance) {
                    continue;
                }
                if (rideEDistance != null && eDistance > rideEDistance) {
                    continue;
                }
            }

            ListingHolder holder = new ListingHolder(listing);
            if (planStartDate.after(tomorrow)) {
                holder.setDayText("明天");
            } else {
                holder.setDayText("今天");
            }
            holder.setStartDistance(sDistance);
            holder.setEndDistance(eDistance);
            holder.setStartTime(startTime.toString());
            holder.setEndTime(endTime.toString());
            holders.add(holder);
        }
        return holders;
    }

}